﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SchoolProject2012.Functions;
using SchoolProject2012.Areas.Shared.Models;

namespace SchoolProject2012.Areas.Shared.Controllers
{
    public class HomeController : Controller
    {
        private SchoolProjectBddEntities db = new SchoolProjectBddEntities();
        //
        // GET: /home/

        public ActionResult Index(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            if (Session["login"] == null)
            {
                if (Request.Browser.IsMobileDevice == false)
                {
                    return View("~/Areas/Shared/Views/Home/Index.cshtml");
                }
                else
                {
                    return View("~/Areas/Shared/Views/Home/Index.Mobile.cshtml");
                }
            }
            else
            {
                string login = Session["login"].ToString();
                ViewBag.area = db.PersonneView.Single(x => x.LOGIN == login).ROLE;
                if (Request.Browser.IsMobileDevice == false)
                {
                    return View("~/Areas/Shared/Views/Home/IndexLogged.cshtml");
                }
                else
                {
                    return View("~/Areas/Shared/Views/Home/IndexLogged.Mobile.cshtml");
                }
            }
        }

        public ViewResult Informations(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            return View();
        }

        // GET: /Shared/home/ContactCDP
        // Formulaire de contact à un chargé de placement
        [AllowAnonymous]
        public ViewResult ContactCDP(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            return View();
        }

        // POST: /Shared/home/ContactCDP
        // Validation du formulaire de contact
        [AllowAnonymous]
        [HttpPost]
        public ActionResult ContactCDP(string langue, string subject, string message, string prenom, string nom)
        {
            try
            {
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
                var cdp = db.CHARGEDEPLACEMENT.ToList().Select(x => x.MAILPERS);
                List<string> to = new List<string>();
                foreach (string email in cdp)
                {
                    to.Add(email);
                }
                Mail.sendMail(subject,prenom+"@"+nom+".fr", to, message);
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
                return View("error");
            }
        }

        

    }
}
